//
//  MineViewController.swift
//  KkmhSwift
//
//  Created by tenltrs on 2018/10/26.
//  Copyright © 2018 Tenltrs. All rights reserved.
//

import UIKit

class MineViewController: BaseViewController,UITableViewDelegate,UITableViewDataSource {
    
    var tableView : UITableView!
    
    var dataArray : [[[String:String]]]!
    
    var headerView : UIView?

    override func viewDidLoad() {
        
        super.viewDidLoad()
        
        initData()

        initMain()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        navigationController?.setNavigationBarHidden(true, animated: true)
    }


    func initData(){

        let menu1 = ["title":"我的消息","icon":"ic_me_item_message_22x22_"]
        let menu2 = ["title":"付费会员","icon":"ic_me_member_22x22_"]
        let menu3 = ["title":"我的频道","icon":"ic_me_channel_22x22_"]
        let menu4 = ["title":"我的关注","icon":"ic_me_item_collection_topic_22x22_"]
        let menu5 = ["title":"我的收藏","icon":"ic_me_item_collection_comic_22x22_"]
        let menu6 = ["title":"浏览历史","icon":"ic_me_item_history_22x22_"]
        
        let menu7 = ["title":"快看商城","icon":"ic_me_item_mall_22x22_"]
        let menu8 = ["title":"快看卡片","icon":"ic_me_card_22x22_"]
        
        let menu9 = ["title":"今日更新","icon":"ic_me_updates_22x22_"]
        let menu10 = ["title":"设置","icon":"ic_me_item_setting_20x20_"]
        
        dataArray=[[menu1,menu2,menu3,menu4,menu5,menu6],[menu7,menu8],[menu9,menu10]]
        
//        RequestManager.POST(url: "https://api.kkmh.com/v2/phone/signin", parameter: ["password":"12345678l","phone":"13164921961"], target: self, isLoad: false, success: { (result) in
//            logger.info(result)
//        }, failure: nil)
    }

    func initMain()  {
        
        let backView = UIViewTool.mas_view(backColor: UIColor.NavColor)
        backView.frame=CGRect(x: 0, y: -ScreenHeight, width: ScreenWidth, height: ScreenHeight)
        
        tableView=UITableView.init(frame: CGRect.zero, style: UITableView.Style.grouped).then({ (make) in
            make.dataSource=self
            make.delegate=self
            
            make.tableHeaderView = MineHeaderView()
            make.sectionFooterHeight=0
            make.separatorStyle = UITableViewCell.SeparatorStyle.none
            
            make.register(cellType: MineTableViewCell.self)
            
            if #available(iOS 11.0, *) {
                make.contentInsetAdjustmentBehavior = .never
            } else {
                automaticallyAdjustsScrollViewInsets = false
            }
            make.contentInset=UIEdgeInsets(top: SafeAreaInsets.top, left: 0, bottom:TABBAR_HEIGHT, right: 0)
            make.scrollToTop()
            make.addSubview(backView)
            
        })
        view.addSubview(tableView)
        tableView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }
    
    // MARK: -- tableView代理
    func numberOfSections(in tableView: UITableView) -> Int {
        return dataArray.count
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return dataArray[section].count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell : MineTableViewCell = tableView.dequeueReusableCell(for: indexPath)
        let arr = dataArray[indexPath.section]
        cell.fillData(dict: arr[indexPath.row], indexPath: indexPath, isSectionLast: indexPath.row >= arr.count-1)
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 5
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        return UIView.init(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: 5))
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 105/2
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        
    }
}
